@echo off
chcp 65001 >nul
echo ========================================
echo 3D立体库货位管理系统 - 安装和启动脚本
echo ========================================
echo.

REM 创建日志文件
set LOG_FILE="%~dp0install-log.log"
echo. > %LOG_FILE%
echo 安装日志开始记录: %date% %time% >> %LOG_FILE%

REM 检查Node.js是否已安装
chcp 65001 >nul
echo 检查Node.js环境...
echo [INFO] 检查Node.js环境... >> %LOG_FILE%
node -v >nul 2>&1
if %errorlevel% neq 0 (
    echo 错误: 未检测到Node.js，请先安装Node.js v16或更高版本
    echo [ERROR] 未检测到Node.js >> %LOG_FILE%
    echo 下载地址: https://nodejs.org/
    echo.
    echo 按任意键打开浏览器访问Node.js下载页面...
    echo [INFO] 打开Node.js下载页面 >> %LOG_FILE%
    pause >nul
    start "" "https://nodejs.org/"
    exit /b 1
)

echo Node.js版本: 
echo [INFO] Node.js版本: >> %LOG_FILE%
for /f "tokens=*" %%i in ('node -v') do set NODE_VERSION=%%i
echo %NODE_VERSION%
echo [INFO] Node.js版本: %NODE_VERSION% >> %LOG_FILE%

echo npm版本: 
echo [INFO] npm版本: >> %LOG_FILE%
for /f "tokens=*" %%i in ('npm -v') do set NPM_VERSION=%%i
echo %NPM_VERSION%
echo [INFO] npm版本: %NPM_VERSION% >> %LOG_FILE%
echo.

REM 设置工作目录为项目根目录（而不是脚本所在目录）
set WORKSPACE=%~dp0..\
echo 工作目录: %WORKSPACE%
echo [INFO] 工作目录: %WORKSPACE% >> %LOG_FILE%
cd /d "%WORKSPACE%"



echo 开始安装项目依赖...
echo [INFO] 开始安装项目依赖 >> %LOG_FILE%
echo.

REM 安装数据库服务依赖
chcp 65001 >nul
echo 安装数据库服务依赖...
echo [INFO] 安装数据库服务依赖 >> %LOG_FILE%
cd /d "%WORKSPACE%database-service"
echo [INFO] 切换到目录: %cd% >> %LOG_FILE%
if exist "package.json" (
    echo 当前目录: %cd%
    echo [INFO] 找到 package.json，开始安装依赖 >> %LOG_FILE%
    call npm install >> %LOG_FILE% 2>&1
    if %errorlevel% neq 0 (
        echo 错误: 数据库服务依赖安装失败
        echo [ERROR] 数据库服务依赖安装失败 >> %LOG_FILE%
        echo 当前目录: %cd%
        echo [INFO] 当前目录: %cd% >> %LOG_FILE%
        echo 请查看 install-log.txt 文件获取详细错误信息
        echo [INFO] 安装过程结束 >> %LOG_FILE%
        pause
        exit /b 1
    ) else (
        echo [INFO] 数据库服务依赖安装成功 >> %LOG_FILE%
    )
) else (
    echo 未找到 database-service/package.json 文件
    echo [WARN] 未找到 database-service/package.json 文件 >> %LOG_FILE%
)
chcp 65001 >nul
echo 数据库服务依赖安装完成
chcp 65001 >nul
echo [INFO] 数据库服务依赖安装完成 >> %LOG_FILE%
echo.

REM 安装主服务器依赖
echo 安装主服务器依赖...
echo [INFO] 安装主服务器依赖 >> %LOG_FILE%
cd /d "%WORKSPACE%server"
echo [INFO] 切换到目录: %cd% >> %LOG_FILE%
if exist "package.json" (
    echo 当前目录: %cd%
    echo [INFO] 找到 package.json，开始安装依赖 >> %LOG_FILE%
    call npm install >> %LOG_FILE% 2>&1
    if %errorlevel% neq 0 (
        echo 错误: 主服务器依赖安装失败
        echo [ERROR] 主服务器依赖安装失败 >> %LOG_FILE%
        echo 当前目录: %cd%
        echo [INFO] 当前目录: %cd% >> %LOG_FILE%
        echo 请查看 install-log.txt 文件获取详细错误信息
        echo [INFO] 安装过程结束 >> %LOG_FILE%
        pause
        exit /b 1
    ) else (
        echo [INFO] 主服务器依赖安装成功 >> %LOG_FILE%
    )
) else (
    echo 未找到 server/package.json 文件
    echo [WARN] 未找到 server/package.json 文件 >> %LOG_FILE%
)
echo 主服务器依赖安装完成
echo [INFO] 主服务器依赖安装完成 >> %LOG_FILE%
echo.

REM 安装PLC服务依赖
echo 安装PLC服务依赖...
echo [INFO] 安装PLC服务依赖 >> %LOG_FILE%
cd /d "%WORKSPACE%plc-service"
echo [INFO] 切换到目录: %cd% >> %LOG_FILE%
if exist "package.json" (
    echo 当前目录: %cd%
    echo [INFO] 找到 package.json，开始安装依赖 >> %LOG_FILE%
    call npm install >> %LOG_FILE% 2>&1
    if %errorlevel% neq 0 (
        echo 错误: PLC服务依赖安装失败
        echo [ERROR] PLC服务依赖安装失败 >> %LOG_FILE%
        echo 当前目录: %cd%
        echo [INFO] 当前目录: %cd% >> %LOG_FILE%
        echo 请查看 install-log.txt 文件获取详细错误信息
        echo [INFO] 安装过程结束 >> %LOG_FILE%
        pause
        exit /b 1
    ) else (
        echo [INFO] PLC服务依赖安装成功 >> %LOG_FILE%
    )
) else (
    echo 未找到 plc-service/package.json 文件
    echo [WARN] 未找到 plc-service/package.json 文件 >> %LOG_FILE%
)
echo PLC服务依赖安装完成
echo [INFO] PLC服务依赖安装完成 >> %LOG_FILE%
echo.

REM 安装队列服务依赖
echo 安装队列服务依赖...
echo [INFO] 安装队列服务依赖 >> %LOG_FILE%
cd /d "%WORKSPACE%queue-service"
echo [INFO] 切换到目录: %cd% >> %LOG_FILE%
if exist "package.json" (
    echo 当前目录: %cd%
    echo [INFO] 找到 package.json，开始安装依赖 >> %LOG_FILE%
    call npm install >> %LOG_FILE% 2>&1
    if %errorlevel% neq 0 (
        echo 错误: 队列服务依赖安装失败
        echo [ERROR] 队列服务依赖安装失败 >> %LOG_FILE%
        echo 当前目录: %cd%
        echo [INFO] 当前目录: %cd% >> %LOG_FILE%
        echo 请查看 install-log.txt 文件获取详细错误信息
        echo [INFO] 安装过程结束 >> %LOG_FILE%
        pause
        exit /b 1
    ) else (
        echo [INFO] 队列服务依赖安装成功 >> %LOG_FILE%
    )
) else (
    echo 未找到 queue-service/package.json 文件
    echo [WARN] 未找到 queue-service/package.json 文件 >> %LOG_FILE%
)
echo 队列服务依赖安装完成
echo [INFO] 队列服务依赖安装完成 >> %LOG_FILE%
echo.

REM 安装规则引擎依赖
echo 安装规则引擎依赖...
echo [INFO] 安装规则引擎依赖 >> %LOG_FILE%
cd /d "%WORKSPACE%rules-engine"
echo [INFO] 切换到目录: %cd% >> %LOG_FILE%
if exist "package.json" (
    echo 当前目录: %cd%
    echo [INFO] 找到 package.json，开始安装依赖 >> %LOG_FILE%
    call npm install >> %LOG_FILE% 2>&1
    if %errorlevel% neq 0 (
        echo 错误: 规则引擎依赖安装失败
        echo [ERROR] 规则引擎依赖安装失败 >> %LOG_FILE%
        echo 当前目录: %cd%
        echo [INFO] 当前目录: %cd% >> %LOG_FILE%
        echo 请查看 install-log.txt 文件获取详细错误信息
        echo [INFO] 安装过程结束 >> %LOG_FILE%
        pause
        exit /b 1
    ) else (
        echo [INFO] 规则引擎依赖安装成功 >> %LOG_FILE%
    )
) else (
    echo 未找到 rules-engine/package.json 文件
    echo [WARN] 未找到 rules-engine/package.json 文件 >> %LOG_FILE%
)
echo 规则引擎依赖安装完成
echo [INFO] 规则引擎依赖安装完成 >> %LOG_FILE%
echo.

REM 安装Node-RED集成依赖
echo 安装Node-RED集成依赖...
echo [INFO] 安装Node-RED集成依赖 >> %LOG_FILE%
cd /d "%WORKSPACE%node-red-integration"
echo [INFO] 切换到目录: %cd% >> %LOG_FILE%
if exist "package.json" (
    echo 当前目录: %cd%
    echo [INFO] 找到 package.json，开始安装依赖 >> %LOG_FILE%
    call npm install >> %LOG_FILE% 2>&1
    if %errorlevel% neq 0 (
        echo 错误: Node-RED集成依赖安装失败
        echo [ERROR] Node-RED集成依赖安装失败 >> %LOG_FILE%
        echo 当前目录: %cd%
        echo [INFO] 当前目录: %cd% >> %LOG_FILE%
        echo 请查看 install-log.txt 文件获取详细错误信息
        echo [INFO] 安装过程结束 >> %LOG_FILE%
        pause
        exit /b 1
    ) else (
        echo [INFO] Node-RED集成依赖安装成功 >> %LOG_FILE%
    )
) else (
    echo 未找到 node-red-integration/package.json 文件
    echo [WARN] 未找到 node-red-integration/package.json 文件 >> %LOG_FILE%
)
echo Node-RED集成依赖安装完成
echo [INFO] Node-RED集成依赖安装完成 >> %LOG_FILE%
echo.

REM 安装前端依赖
echo 安装前端依赖...
echo [INFO] 安装前端依赖 >> %LOG_FILE%
cd /d "%WORKSPACE%client"
echo [INFO] 切换到目录: %cd% >> %LOG_FILE%
if exist "package.json" (
    echo 当前目录: %cd%
    echo [INFO] 找到 package.json，开始安装依赖 >> %LOG_FILE%
    call npm install >> %LOG_FILE% 2>&1
    if %errorlevel% neq 0 (
        echo 错误: 前端依赖安装失败
        echo [ERROR] 前端依赖安装失败 >> %LOG_FILE%
        echo 当前目录: %cd%
        echo [INFO] 当前目录: %cd% >> %LOG_FILE%
        echo 请查看 install-log.txt 文件获取详细错误信息
        echo [INFO] 安装过程结束 >> %LOG_FILE%
        pause
        exit /b 1
    ) else (
        echo [INFO] 前端依赖安装成功 >> %LOG_FILE%
    )
) else (
    echo 未找到 client/package.json 文件
    echo [WARN] 未找到 client/package.json 文件 >> %LOG_FILE%
)
echo 前端依赖安装完成
echo [INFO] 前端依赖安装完成 >> %LOG_FILE%
echo.
chcp 65001 >nul
echo 所有依赖安装完成!

echo [INFO] 所有依赖安装完成 >> %LOG_FILE%
echo.


echo.
echo 详细日志已保存到 install-log.txt 文件中
chcp 65001 >nul
echo [INFO] 安装日志结束记录: %date% %time% >> %LOG_FILE%
echo.
echo 按任意键关闭此窗口...
pause >nul